let UtilsFunctions = {}

UtilsFunctions.AjaxGetCurPositions = (instance, callback) => {
  instance.$http.get('/api/GetCurPositions').then((response) => {
    callback(response.body)
  })
}

UtilsFunctions.AjaxGetExpiredPositions = (instance, callback) => {
  instance.$http.get('/api/GetExpiredPositions').then((response) => {
    callback(response.body)
  })
}

// get position details, instance is the component detail, callback will return the ajax data
// no id needed since it can be achieved from the component instance
UtilsFunctions.AjaxGetPositionsById = (instance, callback) => {
  instance.$http.get(`/api/GetPositionsById/${instance.$route.params.id}`).then((response) => {
    callback(response.body)
  })
}

UtilsFunctions.AjaxUpdatePosition = (instance, options, callback) => {
  let string = optionToString(options)
  console.log(`/api/UpdatePosition?${string}`)
  instance.$http.get(`/api/UpdatePosition?${string}`).then((response) => {
    callback(response.body)
  })
}

UtilsFunctions.AjaxCreatePosition = (instance, options, callback) => {
  let string = optionToString(options)
  console.log(`/api/CreatePosition?${string}`)
  instance.$http.get(`/api/CreatePosition?${string}`).then((response) => {
    callback(response.body)
  })
}

UtilsFunctions.AjaxDeletePosition = (instance, callback) => {
  instance.$http.get(`/api/DeletePosition?id=${instance.position.id}`).then((response) => {
    callback(response.body)
  })
}

let optionToString = (option) => {
  return Object.keys(option).map((k) => {
    return encodeURIComponent(k) + '=' + encodeURIComponent(option[k])
  }).join('&')
}

export default UtilsFunctions
